package com.tedu.weibo2.controller;

import com.tedu.weibo2.mapper.ICommentMapper;
import com.tedu.weibo2.pojo.dto.CommentDTO;
import com.tedu.weibo2.pojo.entity.Comment;
import com.tedu.weibo2.pojo.entity.User;
import com.tedu.weibo2.pojo.vo.CommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment")
public class CommentController {

    @Autowired
    private ICommentMapper iCommentMapper;

    @PostMapping("/insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user==null){
            return 2;//未登录，评论失败
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(user.getId());
        iCommentMapper.insert(comment);
        return 1;//插入成功
    }

    @GetMapping("/selectByWeiboId")
    public List<CommentVO> selectByWeiboId(Integer id){
        return iCommentMapper.selectByWeiboId(id);

    }
}
